package com.seanLab.tool.validator;

import com.seanLab.domain.UserAuthorization;
import com.seanLab.repository.UserAuthorizationRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * Created by ailly on 17-10-7.
 */


@Component
public class DtoValidator {
    @Autowired
    private UserAuthorizationRepo userAuthorizationRepo;

    public static int validateBasicError(Object obj) {
        for (Field f : obj.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            try {
                if (f.get(obj) == null) {
                    return 2;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return 0;
    }

    public int validateAuthorization(String app_id, String app_key) {

//        UserAuthorization ua = new UserAuthorization();
//        ua.setAppId("app1.sina.com.cn");
//        ua.setAppKey("e07dd2a02f81d16238bf5a36d9c21ba4");
//        userAuthorizationRepo.save(ua);

        if (app_id != null && app_id.trim().length() != 0) {
            if (app_key != null && app_key.trim().length() != 0) {
                UserAuthorization userAuthorization = userAuthorizationRepo.findByAppId(app_id);
                if (userAuthorization != null) {
                    if (userAuthorization.getAppKey().equals(app_key)) {
                        return 0;
                    } else {
                        return 1;
                    }
                } else {
                    return 1;
                }
            } else {
                return 1;
            }
        } else {
            return 1;
        }

    }


}
